Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 | 'use client';
/* eslint-disable @typescript-eslint/no-unused-vars */
import React, { useState, useEffect } from 'react';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Textarea } from '@/components/ui/textarea';
import { Switch } from '@/components/ui/switch';
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogTrigger } from '@/components/ui/dialog';
import { Badge } from '@/components/ui/badge';
import { Alert, AlertDescription } from '@/components/ui/alert';
import { Copy, Eye, EyeOff, Key, Plus, RefreshCw, Trash2, Info } from 'lucide-react';
import { masterApiKeyService, MasterApiKeyListItem, MasterApiKey, CreateMasterApiKeyRequest, UpdateMasterApiKeyRequest } from '@/services/masterApiKey';
import { useTranslation } from 'react-i18next';
import useLoadNamespace from '@/hooks/useLoadNamespace';
const MasterApiKeyManagement: React.FC = () => {
useLoadNamespace('admin/masterKeys');
const [keys, setKeys] = useState<MasterApiKeyListItem[]>([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const [isCreateDialogOpen, setIsCreateDialogOpen] = useState(false);
const [isViewDialogOpen, setIsViewDialogOpen] = useState(false);
const [selectedKey, setSelectedKey] = useState<MasterApiKey | null>(null);
const [showFullKey, setShowFullKey] = useState(false);
const { t, i18n } = useTranslation(['admin/masterKeys', 'admin', 'translation']);
// Create form state
const [createForm, setCreateForm] = useState<CreateMasterApiKeyRequest>({
key_name: '',
description: ''
});
useEffect(() => {
loadKeys();
}, []);
const loadKeys = async () => {
setLoading(true);
setError(null);
try {
const result = await masterApiKeyService.getMasterApiKeys();
if (result.success && result.data) {
setKeys(result.data);
} else {
setError(result.error?.details || t('masterKeys.errors.loadFailed'));
}
} catch {
setError(t('masterKeys.errors.loadFailed'));
} finally {
setLoading(false);
}
};
const handleCreateKey = async () => {
if (!createForm.key_name.trim()) {
setError(t('masterKeys.keyNameRequired'));
return;
}
try {
const result = await masterApiKeyService.createMasterApiKey(createForm);
if (result.success && result.data) {
setSelectedKey(result.data);
setIsViewDialogOpen(true);
setIsCreateDialogOpen(false);
setCreateForm({ key_name: '', description: '' });
loadKeys();
} else {
setError(result.error?.details || t('masterKeys.errors.createFailed'));
}
} catch {
setError(t('masterKeys.errors.createFailed'));
}
};
const handleViewKey = async (id: number) => {
try {
const result = await masterApiKeyService.getMasterApiKeyById(id);
if (result.success && result.data) {
setSelectedKey(result.data);
setIsViewDialogOpen(true);
setShowFullKey(false);
} else {
setError(result.error?.details || t('masterKeys.errors.loadSingleFailed'));
}
} catch {
setError(t('masterKeys.errors.loadSingleFailed'));
}
};
const handleToggleEnabled = async (id: number, enabled: boolean) => {
try {
const result = await masterApiKeyService.updateMasterApiKey(id, { enabled });
if (result.success) {
loadKeys();
} else {
setError(result.error?.details || t('masterKeys.errors.updateFailed'));
}
} catch {
setError(t('masterKeys.errors.updateFailed'));
}
};
const handleDeleteKey = async (id: number, keyName: string) => {
if (!confirm(t('masterKeys.confirmDelete', { keyName }))) {
return;
}
try {
const result = await masterApiKeyService.deleteMasterApiKey(id);
if (result.success) {
loadKeys();
if (selectedKey?.id === id) {
setIsViewDialogOpen(false);
setSelectedKey(null);
}
} else {
setError(result.error?.details || t('masterKeys.errors.deleteFailed'));
}
} catch {
setError(t('masterKeys.errors.deleteFailed'));
}
};
const handleRegenerateKey = async (id: number) => {
if (!confirm(t('masterKeys.confirmRegenerate'))) {
return;
}
try {
const result = await masterApiKeyService.regenerateMasterApiKey(id);
if (result.success && result.data) {
setSelectedKey(result.data);
setShowFullKey(true);
loadKeys();
} else {
setError(result.error?.details || t('masterKeys.errors.regenerateFailed'));
}
} catch {
setError(t('masterKeys.errors.regenerateFailed'));
}
};
const copyToClipboard = (text: string) => {
navigator.clipboard.writeText(text);
// You could add a toast notification here
};
const formatDate = (dateString: string) => {
return new Date(dateString).toLocaleString();
};
if (loading) {
return (
<div className="flex items-center justify-center h-64">
<div className="text-lg">{t('masterKeys.loading')}</div>
</div>
);
}
return (
<div className="space-y-6">
{/* Header */}
<div className="flex flex-col sm:flex-row justify-between items-start sm:items-center gap-4">
<div className="space-y-1">
<div className="flex items-center gap-3">
<div className="p-2 bg-primary/10 rounded-lg">
<Key className="h-6 w-6 text-primary" />
</div>
<div>
<h2 className="text-2xl font-bold text-foreground">{t('sidebar.master-keys')}</h2>
<p className="text-muted-foreground">{t('sidebar.description.master-keys')}</p>
</div>
</div>
</div>
<Dialog open={isCreateDialogOpen} onOpenChange={setIsCreateDialogOpen}>
<DialogTrigger asChild>
<Button className="min-w-[180px]">
<Plus className="h-4 w-4 mr-2" />
{t('common.create')} {t('sidebar.master-keys')}
</Button>
</DialogTrigger>
<DialogContent>
<DialogHeader>
<DialogTitle>{t('common.create')} {t('sidebar.master-keys')}</DialogTitle>
<DialogDescription>
{t('masterKeys.createDialogDescription')}
</DialogDescription>
</DialogHeader>
<div className="space-y-6">
<div className="space-y-2">
<Label htmlFor="key_name" className="text-sm font-medium">
{t('common.name')} <span className="text-red-500">*</span>
</Label>
<Input
id="key_name"
value={createForm.key_name}
onChange={(e) => setCreateForm({ ...createForm, key_name: e.target.value })}
placeholder={t('masterKeys.examples.appName')}
className="w-full"
/>
<p className="text-xs text-muted-foreground">
{t('masterKeys.keyNameHelp')}
</p>
</div>
<div className="space-y-2">
<Label htmlFor="description" className="text-sm font-medium">
{t('common.description')}
</Label>
<Textarea
id="description"
value={createForm.description}
onChange={(e) => setCreateForm({ ...createForm, description: e.target.value })}
placeholder={t('masterKeys.descriptionPlaceholder')}
rows={3}
className="w-full resize-none"
/>
<p className="text-xs text-muted-foreground">
{t('masterKeys.descriptionHelp')}
</p>
</div>
<div className="bg-primary/5 border border-border rounded-lg p-4">
<div className="flex items-start space-x-3">
<Info className="h-5 w-5 text-primary mt-0.5" />
<div className="text-sm">
<p className="font-medium text-primary mb-1">{t('masterKeys.securityNoteTitle')}</p>
<p className="text-muted-foreground">
{t('masterKeys.securityNoteDescription')}
</p>
</div>
</div>
</div>
<div className="flex justify-end space-x-3 pt-4 border-t">
<Button variant="outline" onClick={() => setIsCreateDialogOpen(false)}>
{t('common.cancel')}
</Button>
<Button
onClick={handleCreateKey}
disabled={!createForm.key_name.trim()}
className="min-w-[120px]"
>
<Key className="h-4 w-4 mr-2" />
{t('common.create')}
</Button>
</div>
</div>
</DialogContent>
</Dialog>
</div>
{/* Information */}
<div className="bg-gradient-to-r from-primary/5 to-primary/10 border border-border rounded-lg p-4">
<div className="flex items-start space-x-3">
<div className="p-1 bg-primary/10 rounded-full">
<Info className="h-4 w-4 text-primary" />
</div>
<div className="flex-1">
<h3 className="font-medium text-primary mb-1">{t('masterKeys.infoTitle')}</h3>
<p className="text-sm text-muted-foreground leading-relaxed">
{t('masterKeys.infoText')}
</p>
<div className="mt-3 flex flex-wrap gap-2">
<span className="inline-flex items-center px-2 py-1 rounded-full text-xs font-medium bg-primary/10 text-muted-foreground">
{t('masterKeys.badges.externalApps')}
</span>
<span className="inline-flex items-center px-2 py-1 rounded-full text-xs font-medium bg-primary/10 text-muted-foreground">
{t('masterKeys.badges.automationScripts')}
</span>
<span className="inline-flex items-center px-2 py-1 rounded-full text-xs font-medium bg-primary/10 text-muted-foreground">
{t('masterKeys.badges.thirdPartyTools')}
</span>
</div>
</div>
</div>
</div>
{/* Error Display */}
{error && (
<Alert className="border-destructive/20 bg-destructive/5">
<AlertDescription className="text-destructive">{error}</AlertDescription>
</Alert>
)}
{/* Keys List */}
<div className="grid gap-4">
{keys.length === 0 ? (
<Card className="border-dashed border-2">
<CardContent className="flex flex-col items-center justify-center py-12">
<div className="text-center space-y-4">
<div className="p-4 bg-muted rounded-full w-fit mx-auto">
<Key className="h-8 w-8 text-muted-foreground" />
</div>
<div>
<h3 className="text-lg font-medium text-foreground mb-2">{t('masterKeys.empty.title')}</h3>
<p className="text-muted-foreground mb-4">
{t('masterKeys.empty.description')}
</p>
<Button onClick={() => setIsCreateDialogOpen(true)}>
<Plus className="h-4 w-4 mr-2" />
{t('common.create')}
</Button>
</div>
</div>
</CardContent>
</Card>
) : (
keys.map((key) => (
<Card key={key.id} className="hover:shadow-md transition-shadow">
<CardHeader className="pb-3">
<div className="flex justify-between items-start">
<div className="flex-1">
<CardTitle className="flex items-center gap-3 text-lg">
<div className="p-2 bg-primary/10 rounded-lg">
<Key className="h-5 w-5 text-primary" />
</div>
<div className="flex flex-col">
<span>{key.key_name}</span>
<Badge
variant={key.enabled ? "default" : "secondary"}
className="w-fit mt-1"
>
{key.enabled ? t('common.enabled') : t('common.disabled')}
</Badge>
</div>
</CardTitle>
<CardDescription className="mt-2 text-sm">
{key.description || t('masterKeys.noDescription')}
</CardDescription>
</div>
<div className="flex space-x-2">
<Button
variant="outline"
size="sm"
onClick={() => handleViewKey(key.id)}
className="h-9 w-9 p-0"
>
<Eye className="h-4 w-4" />
</Button>
<Button
variant="outline"
size="sm"
onClick={() => handleDeleteKey(key.id, key.key_name)}
className="h-9 w-9 p-0 text-destructive hover:text-destructive hover:bg-destructive/10"
>
<Trash2 className="h-4 w-4" />
</Button>
</div>
</div>
</CardHeader>
<CardContent className="pt-0">
<div className="space-y-3">
{/* API Key Preview */}
<div className="flex min-w-0 items-center justify-between gap-3 p-3 bg-muted rounded-lg">
<span className="shrink-0 text-sm font-medium text-muted-foreground">{t('masterKeys.apiKeyLabel')}</span>
<code className="min-w-0 truncate bg-card border px-3 py-1 rounded text-xs font-mono">
{key.api_key_preview}
</code>
</div>
{/* Metadata Grid */}
<div className="grid grid-cols-2 gap-3 text-sm">
<div>
<span className="text-muted-foreground">{t('masterKeys.labels.created')}:</span>
<p className="font-medium text-foreground">{formatDate(key.created_at)}</p>
</div>
<div>
<span className="text-muted-foreground">{t('masterKeys.labels.lastUsed')}:</span>
<p className="font-medium text-foreground">
{key.last_used_at ? formatDate(key.last_used_at) : t('masterKeys.neverLabel')}
</p>
</div>
</div>
{/* Status Toggle */}
<div className="flex items-center justify-between pt-2 border-t">
<span className="text-sm font-medium text-muted-foreground">{t('masterKeys.activeStatus')}:</span>
<Switch
checked={key.enabled}
onCheckedChange={(checked) => handleToggleEnabled(key.id, checked)}
/>
</div>
</div>
</CardContent>
</Card>
))
)}
</div>
{/* View/Edit Key Dialog */}
<Dialog open={isViewDialogOpen} onOpenChange={setIsViewDialogOpen}>
<DialogContent className="max-w-2xl">
<DialogHeader>
<DialogTitle>{t('masterKeys.detailsTitle')}</DialogTitle>
<DialogDescription>
{t('masterKeys.detailsDescription')}
</DialogDescription>
</DialogHeader>
{selectedKey && (
<div className="space-y-6">
{/* Header Info */}
<div className="flex items-center justify-between p-4 bg-muted rounded-lg">
<div>
<h3 className="text-lg font-semibold text-foreground">{selectedKey.key_name}</h3>
<p className="text-sm text-muted-foreground mt-1">
{selectedKey.description || t('masterKeys.noDescription')}
</p>
</div>
<Badge variant={selectedKey.enabled ? "default" : "secondary"} className="text-sm">
{selectedKey.enabled ? t('common.enabled') : t('common.disabled')}
</Badge>
</div>
{/* API Key Section */}
<div className="space-y-3">
<Label className="text-base font-medium">{t('masterKeys.apiKeyLabel')}</Label>
<div className="flex items-center space-x-3">
<div className="flex-1 relative">
<code className="block w-full bg-muted border rounded-md p-3 font-mono text-sm break-all">
{showFullKey ? selectedKey.api_key : '••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••'}
</code>
</div>
<div className="flex space-x-2">
<Button
variant="outline"
size="sm"
onClick={() => setShowFullKey(!showFullKey)}
className="px-3"
>
{showFullKey ? <EyeOff className="h-4 w-4" /> : <Eye className="h-4 w-4" />}
</Button>
{showFullKey && (
<Button
variant="outline"
size="sm"
onClick={() => copyToClipboard(selectedKey.api_key)}
className="px-3"
>
<Copy className="h-4 w-4" />
</Button>
)}
</div>
</div>
{showFullKey && (
<p className="text-xs text-muted-foreground">
{t('masterKeys.fullAccessWarning')}
</p>
)}
</div>
{/* Metadata */}
<div className="grid grid-cols-1 md:grid-cols-3 gap-4 p-4 bg-muted rounded-lg">
<div>
<Label className="text-sm font-medium text-muted-foreground">{t('masterKeys.labels.created')}</Label>
<p className="text-sm text-foreground mt-1">{formatDate(selectedKey.created_at)}</p>
</div>
<div>
<Label className="text-sm font-medium text-muted-foreground">{t('masterKeys.labels.lastUpdated')}</Label>
<p className="text-sm text-foreground mt-1">{formatDate(selectedKey.updated_at)}</p>
</div>
<div>
<Label className="text-sm font-medium text-muted-foreground">{t('masterKeys.labels.lastUsed')}</Label>
<p className="text-sm text-foreground mt-1">
{selectedKey.last_used_at ? formatDate(selectedKey.last_used_at) : t('masterKeys.neverLabel')}
</p>
</div>
</div>
{/* Actions */}
<div className="flex flex-col sm:flex-row justify-between gap-3 pt-4 border-t">
<div className="flex flex-wrap gap-2">
<Button
variant="outline"
onClick={() => handleRegenerateKey(selectedKey.id)}
className="flex items-center gap-2"
>
<RefreshCw className="h-4 w-4" />
{t('masterKeys.regenerateButton')}
</Button>
<Button
variant="outline"
onClick={() => handleToggleEnabled(selectedKey.id, !selectedKey.enabled)}
className="flex items-center gap-2"
>
{selectedKey.enabled ? t('common.disable') : t('common.enable')}
</Button>
</div>
<Button
variant="destructive"
onClick={() => handleDeleteKey(selectedKey.id, selectedKey.key_name)}
className="flex items-center gap-2"
>
<Trash2 className="h-4 w-4" />
{t('masterKeys.deleteButton')}
</Button>
</div>
</div>
)}
</DialogContent>
</Dialog>
</div>
);
};
export default MasterApiKeyManagement;
|